package com.sanhe.rfmuscle.activity.work;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;


import com.sanhe.rfmuscle.App;
import com.sanhe.rfmuscle.R;
import com.sanhe.rfmuscle.databinding.ActivityRentBinding;
import com.sanhe.rfmuscle.event.CarShowEvent;
import com.sanhe.rfmuscle.event.Event;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class RentActivity extends BaseWorkActivity {
    Typeface medium = Typeface.createFromAsset(App.getApp().getAssets(), "HarmonyOS_Sans_Medium.ttf");
    ActivityRentBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRentBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.title.setTypeface(medium);

        binding.back.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x20));
        binding.ok.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x44));

        binding.on.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x1b));
        binding.off.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x1c));

        binding.rentingOn.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x1c));
        binding.rentingOff.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x1b));

        binding.ok.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    binding.okBg.setImageResource(R.mipmap.shade_bg_press);
                }else if (event.getAction() == MotionEvent.ACTION_UP) {
                    binding.okBg.setImageResource(R.mipmap.shade_bg);
                }
                return false;
            }
        });


        updateState();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CarShowEvent event) {
        updateState();
    }

    private void updateState() {
        if (Event.rentEvent.getValue() == 1) {
            binding.on.setBackgroundResource(R.drawable.black_circle2);
            binding.onBg.setImageResource(R.mipmap.shade_bg_press2);
            binding.off.setBackgroundResource(R.drawable.black_circle);
            binding.offBg.setImageResource(R.mipmap.shade_bg2);
            binding.rentingOff.setVisibility(View.INVISIBLE);
            binding.rentingOn.setVisibility(View.VISIBLE);

        } else {
            binding.on.setBackgroundResource(R.drawable.black_circle);
            binding.onBg.setImageResource(R.mipmap.shade_bg2);
            binding.off.setBackgroundResource(R.drawable.black_circle2);
            binding.offBg.setImageResource(R.mipmap.shade_bg_press2);
            binding.rentingOff.setVisibility(View.VISIBLE);
            binding.rentingOn.setVisibility(View.INVISIBLE);
        }
    }
}
